//
//  BaseViewController.m
//  ArtDog
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "BaseViewController.h"
#import "HLMacro.h"
#import "UIColor+Common.h"
#import "UIImage+Common.h"
#import "UIImageView+WebCache.h"

@interface BaseViewController ()<UIGestureRecognizerDelegate>

@property(nonatomic, copy) KeyboardWillShow keyboardWillShow;
@property(nonatomic, copy) KeyboardWillHide keyboardWillHide;
@property (nonatomic, strong) UIView *largeView;

@end

@implementation BaseViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _isFreeKeyboard = NO;
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _isFreeKeyboard = NO;

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBasic];
    
    [self monitorKeyboard];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)setIsFreeKeyboard:(BOOL)isFreeKeyboard
{
    _isFreeKeyboard = isFreeKeyboard;
    if (isFreeKeyboard) {
        [self createTapGesture];
    }
}

- (void)setIsHaveActivity:(BOOL)isHaveActivity
{
    _isHaveActivity = isHaveActivity;
    if (_largeView == nil) {
        _largeView = [[UIView alloc]initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, self.view.frame.size.height + 64)];
        UIView *activityView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT / 2 - 65, 100, 110)];
        [_largeView addSubview:activityView];
        _largeView.center = self.view.center;
        _largeView.backgroundColor = [UIColor whiteColor];
        activityView.backgroundColor = [UIColor blackColor];
        activityView.alpha = 0.7;
        activityView.layer.cornerRadius = 10;
        
        UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(25, 30, 50, 50)];
        [activityView addSubview:activity];
        activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        [activity startAnimating];
    }
    if (isHaveActivity) {
        [self.view addSubview: _largeView];
    } else {
        [_largeView removeFromSuperview];
    }
}

- (void)setupBasic
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, 64)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]init];
    backItem.title = @"";
    self.navigationItem.backBarButtonItem=backItem;
    
}

#pragma mark - 键盘监听
- (void)monitorKeyboard
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)noti
{
    CGFloat height = 0.0f;
    CGRect rect = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    height = CGRectGetHeight(rect);
    if (_keyboardWillShow != nil) {
        _keyboardWillShow(noti, height);
    }
}

- (void)keyboardWillHide:(NSNotification *)noti
{
    if (_keyboardWillHide != nil) {
        _keyboardWillHide(noti);
    }
}

- (void)setKeyboard:(KeyboardWillShow)keyboardWillShow andWillHide:(KeyboardWillHide)keyboardWillHide
{
    _keyboardWillShow = keyboardWillShow;
    _keyboardWillHide = keyboardWillHide;
}

#pragma mark - 键盘回收
- (void)createTapGesture
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
}

- (void)tapAction
{
    [self resignKeyboard:self.view];
}

- (void)resignKeyboard:(UIView *)supview
{
    for (UIView *subview in supview.subviews) {
        if ([subview isKindOfClass:[UITextField class]] || [subview isKindOfClass:[UITextView class]]) {
            [subview resignFirstResponder];
        } else if (subview.subviews != nil) {
            [self resignKeyboard:subview];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
